//
//  UIDevice.swift
//  MagaCore
//
//  Created by MrMessy on 2023/5/13.
//

import UIKit
import Foundation

extension UIDevice {

  // 屏幕高度
  static func ht_screenHeight() -> CGFloat {
    return UIScreen.main.bounds.size.height
  }

  // 屏幕宽度
  static func ht_screenWidth() -> CGFloat {
    return UIScreen.main.bounds.size.width
  }

  // 适应宽度
  static func ht_fixToWidth(width: CGFloat) -> CGFloat {
    return var_screenWidth / 360 * width
  }

  /// 顶部安全区高度
  static func ht_safeDistanceTop() -> CGFloat {
    if #available(iOS 13.0, *) {
      let var_scene = UIApplication.shared.connectedScenes.first
      guard let var_windowScene = var_scene as? UIWindowScene else { return 0 }
      guard let var_window = var_windowScene.windows.first else { return 0 }
      return var_window.safeAreaInsets.top
    } else if #available(iOS 11.0, *) {
      guard let var_window = UIApplication.shared.windows.first else { return 0 }
      return var_window.safeAreaInsets.top
    }
    return 0
  }

  /// 底部安全区高度
  static func ht_safeDistanceBottom() -> CGFloat {
    if #available(iOS 13.0, *) {
      let var_window = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
      return var_window!.safeAreaInsets.bottom
    } else if #available(iOS 11.0, *) {
      guard let var_window = UIApplication.shared.windows.first else { return 0 }
      return var_window.safeAreaInsets.bottom
    }
    return 0;
  }

  /// 顶部状态栏高度（包括安全区）
  static func ht_statusBarHeight() -> CGFloat {
    var var_statusBarHeight: CGFloat = 0
    if #available(iOS 13.0, *) {
      let var_scene = UIApplication.shared.connectedScenes.first
      guard let var_windowScene = var_scene as? UIWindowScene else { return 0 }
      guard let var_statusBarManager = var_windowScene.statusBarManager else { return 0 }
      var_statusBarHeight = var_statusBarManager.statusBarFrame.height
    } else {
      var_statusBarHeight = UIApplication.shared.statusBarFrame.height
    }
    return var_statusBarHeight == 0 ? 40 : var_statusBarHeight
  }

  /// 导航栏高度
  static func ht_navigationBarHeight() -> CGFloat {
    return 44.0
  }

  /// 状态栏+导航栏的高度
  static func ht_navigationFullHeight() -> CGFloat {
    return UIDevice.ht_statusBarHeight() + UIDevice.ht_navigationBarHeight()
  }

  /// 底部导航栏高度
  static func ht_tabBarHeight() -> CGFloat {
    return 49.0
  }

  /// 底部导航栏高度（包括安全区）
  static func ht_tabBarFullHeight() -> CGFloat {
    return UIDevice.ht_tabBarHeight() + UIDevice.ht_safeDistanceBottom()
  }
}
